﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using WebSite.Controllers;
using WebSite.ViewModels.Fields;

namespace WebSite.ViewModels
{
    public class CompareViewModel
    {
        public Dictionary<Guid, string> Transportadoras { get; set; }
        public string TransportadoraValue { get; set; }
        public Dictionary<Guid, string> Vendedores { get; set; }
        public Dictionary<Guid, string> Fabricas { get; set; }
        public string ClienteValue { get; set; }
        public string VendedorValue { get; set; }
        public string FabricaValue { get; set; }
        public CompareTotal CompareTotal { get; set; }
        public bool AllJolitex { get; set; }
        public IEnumerable<CompareYear> CompareYears { get; set; }
        private readonly double m_MinTotal;
        private readonly double m_MaxTotal;
        private readonly double m_MidTotal;
        private const int RedR = 248;
        private const int RedG = 105;
        private const int RedB = 107;
        private const int YellowR = 255;
        private const int YellowG = 235;
        private const int YellowB = 132;
        private const int GreenR = 99;
        private const int GreenG = 190;
        private const int GreenB = 123;

        public CompareViewModel(IEnumerable<CompareYear> compareYears)
        {
            CompareYears = compareYears;
            m_MinTotal = compareYears.Min(x => x.Min());
            m_MaxTotal = compareYears.Max(x => x.Max());
            m_MidTotal = compareYears.SelectMany(x => x).Average();
        }

        public string GetColor(double? total)
        {
            if (total.HasValue)
            {
                int r;
                int g;
                int b;
                if (total.Value < m_MidTotal)
                {
                    double multiplier = (total.Value - m_MinTotal) / (m_MidTotal - m_MinTotal);
                    r = RedR + (int)((YellowR - RedR) * multiplier);
                    g = RedG + (int)((YellowG - RedG) * multiplier);
                    b = RedB + (int)((YellowB - RedB) * multiplier);
                }
                else
                {
                    double multiplier = (total.Value - m_MidTotal) / (m_MaxTotal - m_MidTotal);
                    r = GreenR + (int)((YellowR - GreenR) * (1 - multiplier));
                    g = GreenG + (int)((YellowG - GreenG) * (1 - multiplier));
                    b = GreenB + (int)((YellowB - GreenB) * (1 - multiplier));
                }
                return String.Format("{0:x2}{1:x2}{2:x2}", r, g, b);
            }
            return "FFFFFF";
        }

        public IHtmlString GetSearchForm(HtmlHelper<CompareViewModel> html, int columns)
        {
            DropDownListField compareTotal = new DropDownListField("Valor", "compareTotal");
            DropDownListField(compareTotal, "Total mercadoria", CompareTotal.TotalMercadoria);
            DropDownListField(compareTotal, "Total liquido", CompareTotal.TotalLiquido);
            DropDownListField(compareTotal, "Numero de pedidos", CompareTotal.Count);
            DropDownListField(compareTotal, "Total de pecas", CompareTotal.TotalPecas);

            return new FormLayoutViewModel(html, columns)
                .AddField(compareTotal)
                .AddField(new TokenInputField("Fábricas", "fabricaIds", FabricaValue, "Search", "SearchFabrica", Fabricas))
                .AddField(new TokenInputField("Vendedores", "vendedorIds", VendedorValue, "Search", "SearchVendedor", Vendedores))
                .AddField(new TokenInputField("Transportadoras", "transportadoraIds", TransportadoraValue, "Search", "SearchTransportadora", Transportadoras))
                .AddField(new AutoCompleteField("Clientes", "cliente", ClienteValue, "SearchCliente", "Search"))
                .AddField(new CheckBoxField("Todas as fabricas Jolitex", "allJolitex", AllJolitex));
        }

        private void DropDownListField(DropDownListField compareTotal, string text, CompareTotal value)
        {
            compareTotal.Add(text, value, CompareTotal == value);
        }
    }
}